<template>
    <div class="light-Ct">
        <!-- 横向Dom -->
        <div class="item-Cd item-Cd-Dom"
            :class="{ 'item-Cd-Dom-line': parentChildrenLength && parentChildrenLength > 1 || (parentChildrenLength && parentChildrenLength > 2 && item.children && item.children.length > 2) }">

            <el-popover placement="top-start" :width="200" trigger="hover">
                <template #reference>
                    <div style="display: inline-block;margin: auto;">
                        <div class="light-circle" style="position: relative;" :class="{
                            'light-green': item.nodeState == 1,
                            'light-red': item.nodeState == 0,
                            'light-orange': item.nodeState == 2,
                        }">
                            <span class="circleTit"> {{ item.percent }} </span>
                        </div>
                        <div class="item-name">
                            <div v-if="!isEditing" @click="startEdit()" class="pointers" style="display: inline-block;">
                                {{
                                    item.nodeName }}</div>
                            <el-input v-else v-model="editedName" @blur="finishEdit" @keyup.enter="finishEdit"
                                ref="editInput" style="width: 150px;" />
                        </div>
                    </div>
                </template>
                <OperationButtons :index="index" :item="item" @add-brother-item="addBrotherItem"
                    @add-child-item="addChildItem" @edit-item="testIntem" @delete-item="deleteItem"
                    @toggle-expand="expandCollapse" @seeNode-Detail="seeNodeDetail" />
            </el-popover>

            <!-- <div>id: {{ item.id }} </div>
            <div>nodeState：{{ item.nodeState }}</div> -->
            <!-- <div>父{{ parentChildrenLength }} 子{{ item.children && item.children.length }}</div> -->

        </div>
        <div style="display: flex;align-items: center;justify-content: center;">
            <div v-if="item.expanded && item.children && item.children.length"
                :class="item.children && item.children.length ? 'childItem childItemSon' : 'childItem'">
                <!-- :style="parentChildrenLength && parentChildrenLength == 1 || item.children && item.children.length == 0 ? 'display: block;' : ''" -->
                <RecursiveItmSX v-for="(child, childIndex) in item.children" :key="childIndex" :item="child"
                    :index="childIndex" @editItem="editItem" @addBrotherItem="addBrotherItem"
                    @addChildItem="addChildItem" @deleteItem="deleteItem" @testIntem="testIntem"
                    @seeNodeDetail="seeNodeDetail" @expandCollapse="expandCollapse"
                    :parentChildrenLength="item.children.length" />
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, defineProps, defineEmits, onUnmounted, nextTick ,watch} from 'vue';
import RecursiveItmSX from './RecursiveItemSX';
import OperationButtons from './OperationButtons.vue';

const props = defineProps({
    item: {
        type: Object,
        required: true
    },
    index: {
        type: Number,
        required: true
    },
    parentChildrenLength: { // 新增 prop
        type: Number,
        default: 0
    }
});

const emit = defineEmits(['testIntem', 'editItem', 'addBrotherItem', 'addChildItem', 'deleteItem', 'seeNodeDetail', 'expandCollapse']);

const isEditing = ref(false);
const editedName = ref(props.item.nodeName);
const editInput = ref(null);

watch(() => props.item,(newItem) => {
        if (newItem) {
            editedName.value = newItem.nodeName;
        }
    },
    { immediate: true } 
);
const startEdit = () => {
    isEditing.value = true;
    nextTick(() => {
        if (isEditing.value && editInput.value) {
            editInput.value.focus();
        }
    });
};

const finishEdit = () => {
    isEditing.value = false;
    if (!editedName.value) {
        return; // ElMessage.error("请输入节点名称");
    }
    if (editedName.value !== props.item.nodeName) {
        emit('editItem', props.index, { ...props.item, nodeName: editedName.value });
    }
};

const testIntem = (index, item) => {
    emit('testIntem', index, item);
};

const editItem = (index, item) => {
    emit('editItem', index, item);
};

const addBrotherItem = (index, item) => {
    emit('addBrotherItem', index, item);
};

const addChildItem = (index, item) => {
    emit('addChildItem', index, item);
};

const deleteItem = (index, item) => {
    emit('deleteItem', index, item);
};

const seeNodeDetail = (index, item) => {
    emit('seeNodeDetail', index, item)
};

const expandCollapse = (index, item) => {
    emit('expandCollapse', index, item);
};

onMounted(() => {

});

onUnmounted(() => {
    editInput.value = null;
});

</script>

<style lang="scss" scoped>
.el-input__inner {
    ime-mode: auto; /* 确保输入法模式正确 */
}
.pointers {
    cursor: pointer;
}

// 横向样式
.light-item {
    .light-Ct {
        .item-Cd {
            padding: 10px;
            box-sizing: border-box;
            position: relative;
            min-width: 76px;
            font-size: var(--global-font-size);

            .light-circle {
                width: 15px;
                height: 15px;
                border-radius: 50%;
                background-color: gray;
                cursor: pointer;
                line-height: 30px;
                margin: auto;
                color: black;
                z-index: 99;
                position: relative;

                &::after {
                    content: "";
                    position: absolute;
                    left: 50%;
                    bottom: -10px;
                    width: 1px;
                    height: 10px;
                    background-color: black;
                }

                &.light-green {
                    background-color: green;
                }

                &.light-red {
                    background-color: red;
                }

                &.light-orange {
                    background-color: orange;
                }

                &.light-gray {
                    background-color: gray;
                }

                .circleTit {
                    position: absolute;
                    top: -7px;
                    left: 20px;
                    font-weight: bold;
                }
            }

            .item-btn {
                a {
                    color: var(--el-color-primary);
                    cursor: pointer;
                }

                span {
                    color: #eee;
                    margin: 0px 4px;
                }
            }

            .item-name {
                margin: 10px 0px;
                color: var(--global-font-color);
                font-size: var(--global-font-size);
                // display: inline-block;
            }

            .operation {
                a {
                    color: var(--global-link-color);
                    cursor: pointer;
                }

                span {
                    color: #eee;
                    // margin: 0px 4px;
                }

                &.operation2 {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    margin: 10px 0px;
                }
            }

        }
    }

    .childItem {
        position: relative;
        display: flex;

        .item-Cd-Dom-line::after {
            content: "";
            position: absolute;
            left: 0px;
            top: 16px;
            width: 100%;
            height: 1px;
            background-color: rgb(43, 38, 38);
        }

        >:first-child>.item-Cd-Dom-line::after {
            content: "";
            position: absolute;
            left: 50% !important;
            top: 16px;
            width: 50%;
            height: 1px;
            background-color: black;
        }

        >:last-child>.item-Cd-Dom-line::after {
            content: "";
            position: absolute;
            left: 0px !important;
            top: 16px;
            width: 50%;
            height: 1px;
            background-color: black;
        }

    }

    .childItemSon>.light-Ct::before {
        content: "";
        position: absolute;
        left: 50%;
        top: -14px;
        width: 1px;
        height: 30px;
        background-color: black;
    }

}

.item-info {
    text-align: left;
    line-height: 20px;
    color: var(--global-font-color);
    font-size: var(--global-font-size);

    p {
        margin: 0px;
    }
}

.light-wrapHXXS {

    .item-name .pointers,
    .circleTit,
    .el-input {
        transform: scaleY(-1);
    }
}
</style>
